<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 850px;
            height: 500px;
            margin: 100px auto;
            position: relative;
        }
        .box1 .pic .ac{
            opacity: 1;
            z-index: 5;
        }
        .box1 .pic img{
            width: 850px;
            height: 500px;
            position: absolute;
            transition: opacity 1.5s;
            opacity: 0;
        }
        .box1 .num span{
            float: left;
            width: 8px;
            height: 8px;
            background: palegreen;
            text-align: center;
            line-height: 20px;
            border-radius: 10px;
            margin-left: 10px;
        }
        .box1 .num {
            width: 100px;
            height: 20px;
            position: absolute;
            left: 366px;
            bottom: 0;
            z-index: 20;
        }
        .box1 .num span{
            cursor: pointer;
        }
        .box1 .num .ac {
            background: palevioletred;
            color: plum;
        }
        .box1 a {
            position: absolute;
            z-index: 20;
            width: 50px;
            height: 50px;
            font-size: 40px;
            text-decoration: none;
            text-align: center;
            line-height: 40px;
            opacity: .5;
            background: #333;
            top: 210px;
            color: palegoldenrod;
        }
        #goPrev{
            left: 0;
        }
        #goNext{
            right: 0;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="pic">
            <img src="img/1.jpg" alt="" class="ac">
            <img src="img/2.jpg" alt="">
            <img src="img/3.jpg" alt="">
            <img src="img/4.jpg" alt="">
            <img src="img/5.jpg" alt="">
        </div>
        <div class="num">
            <span class="ac"></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
        <a href="javascript:;" id="goPrev">&laquo;</a>
        <a href="javascript:;" id="goNext">&raquo;</a>
    </div>
    <script>
        var btn = document.querySelectorAll('span')
        var imgs = document.querySelectorAll('img')
        var goPrev = document.querySelector('#goPrev')
        var goNext = document.querySelector('#goNext')
        var div = document.querySelector('.box1')
        var index = 0;
        var lastindex = 0;
        btn.forEach(function(obj,i){
            obj.onclick = function(){
                lastindex = index
                index = i
                change()
            }
        })

        goPrev.onclick = function(){
            lastindex = index
            index--
            if(index < 0 ){
                index = imgs.length-1
            }
            change()
        }

        goNext.onclick = function(){
            lastindex = index
            index++
            if(index === 5 ){
                index = 0
            }
            change()
        }
        timer()
        function timer(){
           div.ti = setInterval(function(){
                goNext.onclick()
            },2000)
        }
        

        div.onmouseenter = function(){
            clearInterval(div.ti)
        }

        div.onmouseleave = function(){
            timer()
        }

        function change(){
            btn[lastindex].classList.remove('ac')
            btn[index].classList.add('ac')
            imgs[lastindex].classList.remove('ac')
            imgs[index].classList.add('ac')
        }
    </script>
</body>
</html>